package org.usmile.algorithms.huawei.acm;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

// https://fcqian.blog.csdn.net/article/details/128199938
public class _积木最远距离 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] nums = new int[N];
        for (int i = 0; i < N; i++) {
            nums[i] = sc.nextInt();
        }

        Map<Integer, Integer> indexMap = new HashMap<>();
        int max = -1;
        for (int i = 0; i < N; i++) {
            if (indexMap.containsKey(nums[i])) {
                max = Math.max(max, i - indexMap.get(nums[i]));
            } else {
                indexMap.put(nums[i], i);
            }
        }

        System.out.println(max);
    }
}
